﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Reflection;

using Microsoft.SharePoint;

#endregion Using Directives

namespace SharePointStu.SharePoint.General.Logging
{
    public sealed class UILogging
    {
        #region Public Methods

        /// <summary>
        /// Write the error message to the log and display a friendly message to the end user.
        /// </summary>
        /// <param name="container">Container to show the error in.</param>
        /// <param name="clearOtherControls">If other controls in the container control should be removed, leaving only the error control.</param>
        /// <param name="message">Message to write to the log.</param>
        /// <param name="friendlyMessage">Message to show to the user.</param>
        /// <param name="eventId">The event identifier to use when writing to the error log.</param>
        /// <param name="categoryId">The category identifier to use when writing to the error log.</param>
        /// <param name="level">The level of the error.</param>
        public static void WriteError(Control container, bool clearOtherControls, string message, string friendlyMessage, EventId eventId, CategoryId categoryId, ErrorLevel level)
        {
            Logger.LogErrorLocal(eventId, categoryId, level, message, null);

            if (container != null)
            {
                HtmlGenericControl errorControl = new HtmlGenericControl("div");
                errorControl.Attributes.Add("style", "color:red;font-weight:bold;width:100%;padding:5px 5px;");

                if (clearOtherControls)
                {
                    container.Controls.Clear();
                }
                container.Controls.Add(errorControl);

                if (!string.IsNullOrEmpty(errorControl.InnerText) && string.Compare(errorControl.InnerText, friendlyMessage, StringComparison.InvariantCultureIgnoreCase) != 0)
                    friendlyMessage = string.Concat(errorControl.InnerHtml, "<br />", friendlyMessage);

                errorControl.InnerHtml = friendlyMessage;
            }
        }

        /// <summary>
        /// Write the error message to the log and display a friendly message to the end user, removing all other controls in the process.
        /// </summary>
        /// <param name="container">Container to show the error in.</param>
        /// <param name="message">Message to write to the log.</param>
        /// <param name="friendlyMessage">Message to show to the user.</param>
        /// <param name="eventId">The event identifier to use when writing to the error log.</param>
        /// <param name="categoryId">The category identifier to use when writing to the error log.</param>
        /// <param name="level">The level of the error.</param>
        public static void WriteError(Control container, string message, string friendlyMessage, EventId eventId, CategoryId categoryId, ErrorLevel level)
        {
            WriteError(container, true, message, friendlyMessage, eventId, categoryId, level);
        }

        #endregion Public Methods
    }
}
